package jEDF.JEDF;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import jEDF.EDF.*;
import jEDF.FFT.*;
import jEDF.Montage.*;
import jEDF.Dialog.*;
import jEDF.Hypnogram.*;
import jEDF.PreciseScoring.*;

/**
 *
 * <p>Titre : jEDF</p>
 * <p>Description : Java European Data Format Viewer and Analyser</p>
 * <p>Author : Nizar Kerkeni</p>
 * <p>Copyright : Copyright (c) 2003-2006</p>
 * <p>Version 2.0</p>
 */
public class JEDFMenu extends JMenuBar {

    private JMenu fileMenu = new JMenu("File");
    private JMenuItem openEDFFile = new JMenuItem("Open");
    private JMenuItem infoEDFFile = new JMenuItem("Display infos");
    private JMenuItem closeEDFFile = new JMenuItem("Close");
    private JMenuItem quit = new JMenuItem("Quit");
    private JMenu displayMenu = new JMenu("Display");
    private JLabel zoomInGraphs = new JLabel("Zoom in");
    private JLabel zoomOutGraphs = new JLabel("Zoom out");
    private JLabel zoomResetGraphs = new JLabel("Reset zoom");
    private JCheckBoxMenuItem areInverted = new JCheckBoxMenuItem("Invert");
    private JCheckBoxMenuItem areInterpolated = new JCheckBoxMenuItem("Interpolation");
    private JMenu toolsMenu = new JMenu("Tools");
    private JMenuItem edfToTextFile = new JMenuItem("Export to Text");
    private JMenuItem fftAnalysis = new JMenuItem("FFT analysis");
    private JMenu hypnogramMenu = new JMenu("Hypnogram");
    private JMenuItem openHypnogramFile = new JMenuItem("New");
    private JMenuItem loadHypnogramFile = new JMenuItem("Load");
    private JMenuItem hypnogramToTextFile = new JMenuItem("Export to Text");
    private JMenuItem closeHypnogramFile = new JMenuItem("Close");
    private JMenu montageMenu = new JMenu("Montage");
    private JMenuItem newMontage = new JMenuItem("New");
    private JMenuItem defaultMontage = new JMenuItem("Default");
    private JMenuItem presetMontage = new JMenuItem("Preset");
    private JMenuItem M1 = new JMenuItem("Montage 1");
    private JMenuItem M2 = new JMenuItem("Montage 2");
    private JMenuItem M3 = new JMenuItem("Montage 3");
    private JMenuItem M4 = new JMenuItem("Montage 4");
    private JMenu scoringMenu = new JMenu("Precise scoring");
    private JMenuItem newScoring = new JMenuItem("New");
    private JMenuItem loadScoring = new JMenuItem("Load");
    private JMenuItem exportScoring = new JMenuItem("Export to Text");
    private JMenuItem closeScoring = new JMenuItem("Close");
    private JMenu languageMenu = new JMenu("Language");
    private JRadioButtonMenuItem languageEnglish = new JRadioButtonMenuItem("English");
    private JRadioButtonMenuItem langageFrancais = new JRadioButtonMenuItem("Français");
    private JRadioButtonMenuItem linguagemPortuguesBr = new JRadioButtonMenuItem("Português(Brasil)");
    private JMenu helpMenu = new JMenu("Help");
    private JMenuItem kbShortcuts = new JMenuItem("Keyboard shortcuts");
    private JMenuItem about = new JMenuItem("About");
    private Polysomnograph polysomnograph = null;
    private JEDFMainWindow mainWindow = null;
    private JEDFHypnogram hypnogram = null;
    private MontageToolbar montageToolbar = null;
    private PreciseScoring preciseScoring = null;

    /**
     *
     * @param mainWindow JEDFMainWindow
     * @param polysomnograph Polysomnograph
     * @param hypnogram Hypnogram
     * @param montageToolbar MontageToolbar
     * @param preciseScoring PreciseScoring
     */
    public JEDFMenu(JEDFMainWindow mainWindow, Polysomnograph polysomnograph,
            JEDFHypnogram hypnogram, MontageToolbar montageToolbar,
            PreciseScoring preciseScoring) {

        this.mainWindow = mainWindow;
        this.polysomnograph = polysomnograph;
        this.hypnogram = hypnogram;
        this.montageToolbar = montageToolbar;
        this.preciseScoring = preciseScoring;
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.add(fileMenu);
        this.add(displayMenu);
        this.add(toolsMenu);
        this.add(hypnogramMenu);
        this.add(montageMenu);
        this.add(scoringMenu);
        this.add(languageMenu);
        this.add(helpMenu);

        fileMenu.add(openEDFFile);
        fileMenu.add(closeEDFFile);
        fileMenu.add(infoEDFFile);
        fileMenu.addSeparator();
        fileMenu.add(quit);

        displayMenu.add(zoomInGraphs);
        displayMenu.add(zoomOutGraphs);
        displayMenu.add(zoomResetGraphs);
        displayMenu.addSeparator();
        displayMenu.add(areInverted);
        displayMenu.add(areInterpolated);

        hypnogramMenu.add(openHypnogramFile);
        hypnogramMenu.add(loadHypnogramFile);
        hypnogramMenu.add(closeHypnogramFile);
        hypnogramMenu.addSeparator();
        hypnogramMenu.add(hypnogramToTextFile);

        montageMenu.add(newMontage);
        montageMenu.add(defaultMontage);
        montageMenu.add(presetMontage);
        montageMenu.addSeparator();
        montageMenu.add(M1);
        montageMenu.add(M2);
        montageMenu.add(M3);
        montageMenu.add(M4);

        scoringMenu.add(newScoring);
        scoringMenu.add(loadScoring);
        scoringMenu.add(closeScoring);
        scoringMenu.addSeparator();
        scoringMenu.add(exportScoring);

        ButtonGroup bg = new ButtonGroup();
        bg.add(languageEnglish);
        bg.add(langageFrancais);
        bg.add(linguagemPortuguesBr);
        languageMenu.add(languageEnglish);
        languageMenu.add(langageFrancais);
        languageMenu.add(linguagemPortuguesBr);
//        languageEnglish.setSelected(true);
        linguagemPortuguesBr.setSelected(true);
        mainWindow.switchToPortuguesBr();


        helpMenu.add(kbShortcuts);
        helpMenu.addSeparator();
        helpMenu.add(about);

        toolsMenu.add(edfToTextFile);
        toolsMenu.add(fftAnalysis);

        updateTexts();
        addListeners();
    }

    /**
     * enable or disable menu items given the state of the program
     */
    public void updatePermissions() {
        EDFFile edfFile = mainWindow.getEDFFile();
        if (edfFile == null) {
            fileMenu.setEnabled(true);
            openEDFFile.setEnabled(true);
            infoEDFFile.setEnabled(false);
            closeEDFFile.setEnabled(false);
            toolsMenu.setEnabled(false);
            areInterpolated.setSelected(false);
            areInverted.setSelected(false);
            displayMenu.setEnabled(false);
            hypnogramMenu.setEnabled(false);
            montageMenu.setEnabled(false);
            scoringMenu.setEnabled(false);
        } else {
            openEDFFile.setEnabled(false);
            infoEDFFile.setEnabled(true);
            closeEDFFile.setEnabled(true);
            toolsMenu.setEnabled(!edfFile.isItAHypnogramRecord());
            displayMenu.setEnabled(!edfFile.isItAHypnogramRecord());

            hypnogramMenu.setEnabled(true);
            openHypnogramFile.setEnabled(hypnogram.isCleared());
            loadHypnogramFile.setEnabled(hypnogram.isCleared());
            hypnogramToTextFile.setEnabled(!hypnogram.isCleared());
            closeHypnogramFile.setEnabled(!hypnogram.isCleared());

            montageMenu.setEnabled(!edfFile.isItAHypnogramRecord());
            presetMontage.setEnabled(Montage.isPresetAvailable());

            scoringMenu.setEnabled(!hypnogram.isCleared());
            newScoring.setEnabled(!hypnogram.isCleared() && preciseScoring.isCleared());
            loadScoring.setEnabled(!hypnogram.isCleared() && preciseScoring.isCleared());
            exportScoring.setEnabled(!hypnogram.isCleared() && !preciseScoring.isCleared());
            closeScoring.setEnabled(!hypnogram.isCleared() && !preciseScoring.isCleared());
        }
    }

    private void addListeners() {
        openEDFFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.openEDFFile();
            }
        });

        infoEDFFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                new EDFInformationDialog(mainWindow);
            }
        });

        closeEDFFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.close();
            }
        });

        quit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.quit();
            }
        });

        edfToTextFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new EDFSignalToTextDialog(mainWindow, polysomnograph);
            }
        });

        fftAnalysis.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new FFTExportDialog(mainWindow, polysomnograph);
            }
        });

        zoomInGraphs.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent arg0) {
                polysomnograph.zoomIn();
            }

            public void mouseEntered(MouseEvent arg0) {
                zoomInGraphs.setForeground(Color.red);
            }

            public void mouseExited(MouseEvent arg0) {
                zoomInGraphs.setForeground(Color.black);
            }
        });

        zoomOutGraphs.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent arg0) {
                polysomnograph.zoomOut();
            }

            public void mouseEntered(MouseEvent arg0) {
                zoomOutGraphs.setForeground(Color.red);
            }

            public void mouseExited(MouseEvent arg0) {
                zoomOutGraphs.setForeground(Color.black);
            }
        });

        zoomResetGraphs.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent arg0) {
                polysomnograph.zoomReset();
            }

            public void mouseEntered(MouseEvent arg0) {
                zoomResetGraphs.setForeground(Color.red);
            }

            public void mouseExited(MouseEvent arg0) {
                zoomResetGraphs.setForeground(Color.black);
            }
        });

        openHypnogramFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new NewHypnogramDialog(mainWindow, polysomnograph, hypnogram, false);
                mainWindow.updatePermissions();
            }
        });

        loadHypnogramFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new NewHypnogramDialog(mainWindow, polysomnograph, hypnogram, true);
                mainWindow.updatePermissions();
            }
        });

        hypnogramToTextFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.exportHypnogramToText();
            }
        });

        closeHypnogramFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.closeHypnogram();
            }
        });

        newMontage.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                montageToolbar.clickNewMontage();
            }
        });

        defaultMontage.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                montageToolbar.clickMontageNone();
            }
        });

        presetMontage.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                montageToolbar.clickPresetMontage();
            }
        });

        M1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                montageToolbar.clickM1();
            }
        });

        M2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                montageToolbar.clickM2();
            }
        });

        M3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                montageToolbar.clickM3();
            }
        });

        M4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                montageToolbar.clickM4();
            }
        });

        newScoring.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new NewPreciseScoringDialog(mainWindow, preciseScoring, false);
            }
        });

        loadScoring.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new NewPreciseScoringDialog(mainWindow, preciseScoring, true);
            }
        });

        exportScoring.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new PreciseScoringToTextDialog(mainWindow, preciseScoring);
            }
        });

        closeScoring.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                preciseScoring.clear();
                mainWindow.updatePermissions();
            }
        });

        languageEnglish.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.switchToEnglish();
                updateTexts();
            }
        });

        langageFrancais.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.switchToFrench();
                updateTexts();
            }
        });
        linguagemPortuguesBr.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.switchToPortuguesBr();
                updateTexts();
            }
        });

        kbShortcuts.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                new JEDFHelpDialog(mainWindow);
            }
        });

        areInverted.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                mainWindow.setsignalInverted(areInverted.isSelected());
                polysomnograph.setAllGraphsInverted(areInverted.isSelected());
            }
        });

        areInterpolated.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                polysomnograph.setInterpolated(areInterpolated.isSelected());
            }
        });

        displayMenu.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                if (areInterpolated.isEnabled() !=
                        polysomnograph.isInterpolated()) {
                    areInterpolated.setEnabled(polysomnograph.isInterpolated());
                }
            }
        });

        about.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                new JEDFAboutDialog(mainWindow, true);
            }
        });
    }

    /**
     * update this menu text when the application langage has been modified
     */
    public void updateTexts() {
        fileMenu.setText(JEDFMainWindow.getLocalizedString("File"));
        openEDFFile.setText(JEDFMainWindow.getLocalizedString("Open"));
        infoEDFFile.setText(JEDFMainWindow.getLocalizedString("Informations"));
        closeEDFFile.setText(JEDFMainWindow.getLocalizedString("Close"));
        quit.setText(JEDFMainWindow.getLocalizedString("Quit"));

        displayMenu.setText(JEDFMainWindow.getLocalizedString("Display"));
        zoomInGraphs.setText(JEDFMainWindow.getLocalizedString("Zoom in"));
        zoomOutGraphs.setText(JEDFMainWindow.getLocalizedString("Zoom out"));
        zoomResetGraphs.setText(JEDFMainWindow.getLocalizedString("Reset zoom"));
        areInverted.setText(JEDFMainWindow.getLocalizedString("Invert"));
        areInterpolated.setText(JEDFMainWindow.getLocalizedString("Interpolation"));

        toolsMenu.setText(JEDFMainWindow.getLocalizedString("Tools"));
        edfToTextFile.setText(JEDFMainWindow.getLocalizedString("Export to Text"));
        fftAnalysis.setText(JEDFMainWindow.getLocalizedString("FFT analysis"));

        hypnogramMenu.setText(JEDFMainWindow.getLocalizedString("Hypnogram"));
        openHypnogramFile.setText(JEDFMainWindow.getLocalizedString("New"));
        loadHypnogramFile.setText(JEDFMainWindow.getLocalizedString("Load"));
        hypnogramToTextFile.setText(JEDFMainWindow.getLocalizedString("Export to Text"));
        closeHypnogramFile.setText(JEDFMainWindow.getLocalizedString("Close"));

        newMontage.setText(JEDFMainWindow.getLocalizedString("New"));
        defaultMontage.setText(JEDFMainWindow.getLocalizedString("Default"));
        presetMontage.setText(JEDFMainWindow.getLocalizedString("Preset"));

        scoringMenu.setText(JEDFMainWindow.getLocalizedString("Precise Scoring"));
        newScoring.setText(JEDFMainWindow.getLocalizedString("New"));
        loadScoring.setText(JEDFMainWindow.getLocalizedString("Load"));
        exportScoring.setText(JEDFMainWindow.getLocalizedString("Export to Text"));
        closeScoring.setText(JEDFMainWindow.getLocalizedString("Close"));

        languageMenu.setText(JEDFMainWindow.getLocalizedString("Language"));

        kbShortcuts.setText(JEDFMainWindow.getLocalizedString("Keyboard shortcuts"));
        about.setText(JEDFMainWindow.getLocalizedString("About"));
        helpMenu.setText(JEDFMainWindow.getLocalizedString("Help"));
        montageMenu.setText(JEDFMainWindow.getLocalizedString("Montage"));
    }
}
